package com.tdg.qywx.controller;

import com.alibaba.fastjson.JSONObject;
import com.tdg.qywx.utils.HttpClientUtil;
import com.tdg.qywx.utils.WeiXinUtils2;
import io.swagger.v3.oas.annotations.Hidden;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/tdg/qywx/api2/v1")
public class ShareController {
    //企业微信应用
    public static final String APPID = "wwf4e0e3325668447b";

    public static String APP_SECRET_CX;

    public static String getAppSecretCx() {
        return ShareController.APP_SECRET_CX;
    }

    @Value("${wx.app.secret}")
    public void setAppSecretCx(String appSecretCx) {
        ShareController.APP_SECRET_CX = appSecretCx;
    }

    public static String SHARE_URL;
    public static String getShareUrl() {
        return ShareController.SHARE_URL;
    }

    @Value("${SHARE_URL}")
    public void setShareUrl(String shareUrl) {
        ShareController.SHARE_URL = shareUrl;
    }

    //正式环境
//    public static final String APP_SECRET_CX ="lc2sAPu2BmXQBEdgZHk3HLlWdXvEKE_7VUVDHzG7UOE";
//    public static final String SHARE_URL = "redirect:https://oa.tdgcore.com:8870/tdg/mocks/cxrytest/shouye2.jsp?userId=";

    //测试环境
//    public static final String APP_SECRET_CX ="CueUgSYVWH5s7Eelz1xHfEV4fTqq91xor7FINEoJ_zY";
//    public static final String SHARE_URL = "redirect:https://oa.tdgcore.com:8870/tdg/mocks-test/cxrytest/shouye2.jsp?userId=";

    private static final Map map = new HashMap();

    @Hidden
    @RequestMapping("/share")
    public JSONObject share(HttpServletRequest request, @RequestParam String urlSigna) throws IOException {
        JSONObject j = new JSONObject();
        WeiXinUtils2 wx = new WeiXinUtils2();
        urlSigna = URLDecoder.decode(urlSigna, "UTF-8");
        Map<String, Object> wxMap = wx.getSignature(urlSigna);
        j.put("obj",wxMap);
        return j;
    }

    @RequestMapping(value="/callbackByCX")
    public ModelAndView callbackByCX(HttpServletRequest request) throws UnsupportedEncodingException {
        String code = request.getParameter("code");
        String access_token = WeiXinUtils2.getAccess_token(APPID,getAppSecretCx());

        String url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=" + access_token + "&code=" + code;
        net.sf.json.JSONObject jsonobject = HttpClientUtil.sendGet(url, null);
        String userId = (String) jsonobject.get("UserId");
        String url2 = "https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token=" + access_token + "&userid=" + userId;
        net.sf.json.JSONObject jsonobject2 = HttpClientUtil.sendGet(url2, null);
        String userName = (String) jsonobject2.get("name");
        System.out.println(userName+"++++登录成功");

        return new ModelAndView(getShareUrl() + userId+"&userName="+ URLEncoder.encode(userName,"UTF-8"));
    }

}
